package controller.android;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserDTO;
import bizlogic.UserManager;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/AndroidLogin")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LoginServlet() {
        super();
    }

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doLogin(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doLogin(request, response);
	}

	private void doLogin(HttpServletRequest req, HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		UserManager um = new UserManager();
		UserDTO user = null;
				
		user = um.doLogin(req.getParameter("username"), req.getParameter("password"));
		
		if ( user == null ) { // Not valid user
			out.println("false");
		} else { // Valid user
			out.println("true");
			out.println(user.getId());
			out.println(user.getType());
		}
		
	}

}
